import axios from "axios";

import {message} from "ant-design-vue";
const $http = axios.create({
  // baseURL: "http://localhost:8080/", //接口统一域名
  timeout: 10000, //设置超时
});

//-----------------------------、请求拦截器 忽略
$http.interceptors.request.use(
  function (config: any) {
    // console.log(config);
    config.headers.Authorization = localStorage.getItem("token");

    return config;
  },
  function (error) {
    // 对请求错误做些什么

    return Promise.reject(error);
  }
);

//----------------- 二、响应拦截器 忽略
$http.interceptors.response.use(
  function (response) {
    if (response.data.code === 200) {
      return response.data.data;
    } else {
        if (response.data.code === 401) {
            localStorage.clear()
            window.location.href = "/";

        }else{
            message.error(response.data.msg)
        }

        return Promise.reject("数据出错");
    }
  },
  function (error) {
    // 对响应错误做点什么
    console.log("拦截器报错");
    return Promise.reject(error);
  }
);

export default $http;
